/********************************************
 * 功能说明: Lessee实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2020-03-24 10:09:23
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.operation.tenant.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.repository.LesseeContactRepository;
import com.spring.arch.operation.tenant.repository.LesseeRepository;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.errorcode.TenantErrorCode;
import com.spring.arch.operation.tenant.model.Lessee;
import com.spring.arch.operation.tenant.model.LesseeContact;
import com.spring.arch.operation.tenant.model.LesseeInvoice;
import com.spring.arch.operation.tenant.repository.LesseeContactRepository;
import com.spring.arch.operation.tenant.repository.LesseeInvoiceRepository;
import com.spring.arch.operation.tenant.repository.LesseeRepository;
import com.spring.arch.operation.tenant.web.dto.WebLesseeDTO;
import lombok.NonNull;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import java.util.List;

/**
 * Lessee实体与DTO之间接口数据转换实现
 * @author
 * @version 1.0.0.1
 * @since 2020-03-24 10:09:23
 */
@Component
public class WebLesseeConverter extends AbstractConverter<Lessee, WebLesseeDTO> {

    @Autowired
    private LesseeRepository lesseeRepository;
    @Autowired
    private LesseeContactRepository lesseeContactRepository;
    @Autowired
    private LesseeInvoiceRepository lesseeInvoiceRepository;
    @Autowired
    private WebLesseeContactConverter lesseeContactConverter;
    @Autowired
    private WebLesseeInvoiceConverter lesseeInvoiceConverter;
    @Autowired
    private EntityManager entityManager;

    @Override
    public Lessee toModel(@NonNull final WebLesseeDTO dto) {
        if (dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebLesseeDTO toDTO(@NonNull final Lessee model) {
        final WebLesseeDTO dto = new WebLesseeDTO();

        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setType(model.getType());
        dto.setCustomerIdentifier(model.getCustomerIdentifier());
        dto.setIndustry(model.getIndustry());
        dto.setFoundingTime(model.getFoundingTime());
        dto.setSocialCreditCode(model.getSocialCreditCode());
        dto.setRegisteredCapital(model.getRegisteredCapital());
        dto.setLegalPerson(model.getLegalPerson());
        dto.setTelephone(model.getTelephone());
        dto.setAddress(model.getAddress());
        dto.setPostalAddress(model.getPostalAddress());

        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private Lessee constructModel(final WebLesseeDTO dto) {
        final Lessee model = new Lessee();
        this.dtoToModel(dto, model);
        return model;
    }

     /**
      * 更新Model
      */
    private Lessee updateModel(final WebLesseeDTO dto) {
        final Lessee model = this.lesseeRepository.findById(dto.getId())
                        .orElseThrow(() -> ExceptionHolder.serviceException(TenantErrorCode.E1201000001, dto.getId().toString()));
        entityManager.unwrap(Session.class).evict(model);
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebLesseeDTO dto, final Lessee model) {
        model.setName(dto.getName());
        model.setType(dto.getType());
        model.setCustomerIdentifier(dto.getCustomerIdentifier());
        model.setIndustry(dto.getIndustry());
        model.setFoundingTime(dto.getFoundingTime());
        model.setSocialCreditCode(dto.getSocialCreditCode());
        model.setRegisteredCapital(dto.getRegisteredCapital());
        model.setLegalPerson(dto.getLegalPerson());
        model.setTelephone(dto.getTelephone());
        model.setAddress(dto.getAddress());
        model.setPostalAddress(dto.getPostalAddress());
    }

    /**
     * Model转换到DTO（详细和编辑视图使用）
     *
     * @param lessee
     */
    @Override
    public WebLesseeDTO toDTOForView(Lessee lessee) {
        WebLesseeDTO lesseeDTO = this.toDTO(lessee);
        List<LesseeContact> contacts = this.lesseeContactRepository.findAllByLessee(lessee);
        if(CollectionUtils.isNotEmpty(contacts)) {
            lesseeDTO.setLesseeContacts(this.lesseeContactConverter.toListDTO(contacts));
        }

        List<LesseeInvoice> invoices = this.lesseeInvoiceRepository.findAllByLessee(lessee);
        if(CollectionUtils.isNotEmpty(invoices)) {
            lesseeDTO.setLesseeInvoices(this.lesseeInvoiceConverter.toListDTO(invoices));
        }
        return lesseeDTO;
    }
}
